<%@page pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<!-- 
  - Author(s): YANGZHOU
  - Date: 2013-03-01 17:43:27 
  - Description:
-->
<head>
	<%@include file="/common.jsp"%>
	<%@include file="/common/common.jsp"%>
	<%@include file="/coframe/tools/skins/common.jsp" %>
	<title>组织机构树</title>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<script src="<%= request.getContextPath() %>/common/nui/nui.js" type="text/javascript"></script>
	
</head>
<body>
	<div class="nui-fit" style="width:100%;height:100%;">
		<table style="width:100%;height:89%;">
	    	<tr>
	      		<td>所有机构</td>
	         	<td></td>
	        	<td>可管理机构</td>
	      	</tr>
	      	<tr>
		      	<td style="width:40%;height:90%;border:1px solid #878787;vertical-align:top;">
		        	<ul id="tree1" class="nui-tree" style="width:100%;" url="org.gocom.components.coframe.org.organization.getOrgStaticTree.biz.ext"
			          dataField="treeNodes" showTreeIcon="true" textField="nodeName" idField="nodeId" resultAsTree="false" parentField="parentId"
			          onnodedblclick="onNodeDbClick">
			        </ul>
		        </td>
		        <td style="width:20%;height:90%;">
		            <table style="width:100%;">
				    	<tr>
				    		<td align="center">
				    	  		<input type="button" id="addBtn" text="添加" style="width:65px;text-align:center;" class="nui-button" onclick="addOrgResource"/>
				    	  	</td>
				    	</tr>
						<tr>
						  	<td align="center">&nbsp;</td>
						</tr>
							<tr>
						  		<td align="center">
						    		<input type="button" id="allDeleteBtn" text="全部删除" class="nui-button" style="width:65px;text-align:center;" onclick="deleteAllOrgResource"/>
						  		</td>
							</tr>
						<tr>
						  	<td align="center">
						   		<input type="button" id="deleteBtn" text="删除" class="nui-button" style="width:65px;text-align:center;" onclick="deleteOrgResource" />
						  	</td>
						</tr>
					</table>
		        </td>
		        <td style="width:40%;height:90%;">
		            <div class="nui-fit">
		              	<div id="listbox1" class="nui-listbox" style="width:100%;height:100%;"
		               		textField="nodeName" valueField="nodeId" multiSelect="true">
		              	</div>
		            </div>
		        </td>
		   	</tr>
		</table>
	    <div class="nui-toolbar" style="text-align:center;padding-top:8px;padding-bottom:8px;" borderStyle="border:0;">
	        <a class="nui-button" style="width:60px;" iconCls="icon-ok" onclick="onOk()">确定</a>
	        <span style="display:inline-block;width:25px;"></span>
	        <a class="nui-button" style="width:60px;" iconCls="icon-cancel" onclick="onCancel()">取消</a>
		</div>
	</div>
	
	<script type="text/javascript">
	    nui.parse();
	    
	    var tree = nui.get("tree1");//所有机构
	    var listbox = nui.get("listbox1");//可管理机构
	    
	    //添加按钮-添加到可管理机构
	    function addOrgResource(){
	    	//获取节点
	        var node = tree.getSelectedNode();
	        if(!node){
	            nui.alert("请选择一个机构","提示");
	        }
	        //获取父节点
	        var parentNodes = tree.getAncestors(node);
	        var items = listbox.getData();
	        //判断是否已拥有对该机构的管理权限
	        for(var i=0,len=items.length;i<len;i++){
	            if(node.nodeId==items[i].nodeId){
	                //nui.alert("已拥有对该机构的管理权限","提示");
	                return;
	            }
	            for(var j=0,len1=parentNodes.length;j<len1;j++){
		            if(parentNodes[j].nodeId==items[i].nodeId){
		                //nui.alert("已拥有对该机构的父机构的管理权限","提示");
		                return;
		            }
	            }
	        }
	        //点击父节点时，如果子节点存在于已拥有管理权限，删除子节点，添加父节点
	        tree.cascadeChild(node, function (childNode) {
	            for(var i=0,len=items.length;i<len;i++){
	                if(childNode.nodeId==items[i].nodeId){
	                    listbox.removeItems([items[i]]);
	                }
	            }
	        });
			//添加节点到已拥有管理权限
	        listbox.addItem(node);
	    }
	    
	    //双击节点
	    function onNodeDbClick(){
	        addOrgResource();
	    }
	    
	    // 页面数据取得
	    function SetData(data){
	       data = nui.clone(data);
	       var ids = data.ids.split(",");
	       var texts = data.texts.split(",");
	       if(ids!=""){
		       for(var i=0;i<ids.length;i++){
		          var id = ids[i].split(":");
		          var node ={
		             nodeId:id[0],
		             nodeName:texts[i]
		          };
		          listbox.addItem(node);
		       }
	       }
	    }
	    
	    //获取选中
		function GetData() {
			var items = listbox.getData();
			if(items.length == 1){
				var ids = [];
				var texts = [];
				for(var i=0;i<items.length;i++){
					ids.push(items[i].nodeId);
					texts.push(items[i].nodeName);
				}
		        return {id:ids.join(","),text:texts.join(",")};
	        }else{
	        	nui.alert("只能选择一个可管理机构！","提示");
	        }
	    }
	    
	    //删除按钮-删除可管理机构
	    function deleteOrgResource(){
	        var nodes = listbox.getSelecteds();
	        if(nodes.length==0){
	            nui.alert("请选择至少一个可管理机构!","提示");
	            return;
	        }
	        listbox.removeItems(nodes);
	    }
	    
	    //全部删除按钮-删除所有可管理机构
	    function deleteAllOrgResource(){
	        listbox.removeAll();
	    }
	    
	    //确定
	    function onOk() {
	        CloseWindow("ok");
	    }
	    
	    //取消
	    function onCancel() {
	        CloseWindow("cancel");
	    }
	    
	    //关闭事件
		function CloseWindow(action) {
	        if (window.CloseOwnerWindow) return window.CloseOwnerWindow(action);
	        else window.close();
	    }
	</script>
</body>
</html>
